package com.uking.util;

import android.util.Base64;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class DES {
    private static byte[] iv = new byte[]{(byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0};

    public static String encryptDESURL(String encryptString, String encryptKey) throws Exception {
        IvParameterSpec zeroIv = new IvParameterSpec(iv);
        SecretKeySpec key = new SecretKeySpec(encryptKey.getBytes(), "DES");
        Cipher cipher = Cipher.getInstance("DES");
        cipher.init(1, key, zeroIv);
        return Base64.encodeToString(cipher.doFinal(encryptString.getBytes()), 8);
    }

    public static String decryptDESURL(String decryptString, String decryptKey) throws Exception {
        byte[] byteMi = Base64.decode(decryptString, 8);
        IvParameterSpec zeroIv = new IvParameterSpec(iv);
        SecretKeySpec key = new SecretKeySpec(decryptKey.getBytes(), "DES");
        Cipher cipher = Cipher.getInstance("DES");
        cipher.init(2, key, zeroIv);
        return new String(cipher.doFinal(byteMi));
    }
}
